/*
 * @Author: 'zhangkeqq' '2211709169@qq.com'
 * @Date: 2025-06-20 21:52:23
 * @LastEditors: your name
 * @LastEditTime: 2025-07-05 14:52:18
 * @FilePath: \vue3-admin\mock\user.ts
 * @Description: 模拟用户相关的mock接口
 */
// 模拟用户列表数据
const createUserList= ()=> [
  {
    userId: 1,
    avatar:'/public/logo.png',
    username: 'admin',
    password: '123456',
    desc: '超级管理员',
    roles: ['平台管理员'],
    buttons: [],
    routes:['Acl','User','Role','Permission', 'Product','Trademark','Attr','Sku','Spu'],
    token:'Admin Token1',
  },
  {
    userId: 2,
    avatar:'/public/logo.png',
    username: 'admin2',
    password: '111111',
    desc: '超级管理员2',
    roles: ['平台管理员2'],
    buttons: [],
    routes:['Product','Trademark','Attr','Sku','Spu'],
    token:'Admin Token2'
  },
    {
    userId: 3,
    avatar:'/public/logo.png',
    username: 'test1',
    password: '111111',
    desc: '普通用户',
    roles: ['普通用户'],
    buttons: [],
    routes:[],
    token:'Admin Token3'
  },
]
// 模拟用户列表数据
const userList = [
  {
    id: 1,
    username: 'admin',
    password: '123456',
    name: '超级管理员',
    phone: '13800138000',
    roleName: '超级管理员',
    createTime: '2024-03-21',
    updateTime: '2024-03-21',
    status: 1,
  },
  {
    id: 2,
    username: 'test',
    password: '123456',
    name: '测试用户',
    phone: '13800138001',
    roleName: '普通管理员',
    createTime: '2024-03-21',
    updateTime: '2024-03-21',
    status: 1,
  },
]
export default[
      // 批量删除用户
  {
    url: '/acl/user/batchRemove',
    method: 'post',
    response: ( options) => {
     const idList = options.body.data // 假设请求体中包含要删除的ID数组
     idList?.forEach((id) => {
        const index = userList.findIndex((item) => item.id === id)
        if (index !== -1) {
          userList.splice(index, 1)
        }
      })
      return { code: 200, data:  {deletedIds: options}, message: '批量删除成功' }
    },
  },
      // 获取用户角色
  {
    url: '/acl/user/toAssign/:userId',
    method: 'get',
    response: () => {
      return {
        code: 200,
        data: {
          assignRoles: [
            {
              id: 1,
              roleName: '超级管理员',
              createTime: '2024-03-21',
              updateTime: '2024-03-21',
            },
          ],
          allRolesList: [
            {
              id: 1,
              roleName: '超级管理员',
              createTime: '2024-03-21',
              updateTime: '2024-03-21',
            },
            {
              id: 2,
              roleName: '普通管理员',
              createTime: '2024-03-21',
              updateTime: '2024-03-21',
            },
          ],
        },
      }
    },
  },
    // 分配用户角色
  {
    url: '/acl/user/doAssignRole',
    method: 'post',
    response: () => {
      return { code: 200, data: null, message: '分配角色成功' }
    },
  },
    // 用户登录接口
     {
        url: '/admin/acl/index/login',
        method: 'post',
        response: ({ body }: any) => {
            // 获取请求体携带过来的用户名与密码
            const { username, password } = body
            // 调用获取用户信息函数，用于判断是否有此用户
            const checkUser = createUserList().find(
                (item) => item.username === username && item.password === password
            )
            // 没有用户则返回失败信息
            if (!checkUser) {
                return {
                    code: 201,
                    data: {
                        message: '账号或者密码不正确'
                    }
                }
            }
            const {token} = checkUser
            return {
                code: 200,
                data: {
                    token
                }
            }
        }
    },

    // 获取用户信息
        {
        url: '/admin/acl/index/info',
        method: 'get',
        response: (request: any) => {
            // 获取请求头携带的 token
            const token = request.headers.token
            // 查看用户信息数据中是否包含有此 token 的用户
            const checkUser = createUserList().find((item) => item.token === token)
            // 没有就返回失败信息
            if (!checkUser) {
                return {
                    code: 201,
                    data: {
                        message: '获取用户信息失败'
                    }
                }
            }
            // 有就返回成功信息
            return {
                code: 200,
                data: {
                    checkUser
                }
            }
        }
    },
      // 用户登出接口
  {
    url: '/admin/acl/index/logout',
    method: 'post',
    response: () => {
      return { code: 200, data: null, message: '退出成功' }
    },
  },
    // 获取用户列表
  {
    url: '/acl/user/:page/:limit',
    method: 'get',
    response: ({ query }) => {
      const { username } = query
      let filteredList = userList
      if (username) {
        filteredList = userList.filter((user) =>
          user.username.includes(username),
        )
      }
      return {
        code: 200,
        data: {
          records: filteredList,
          total: filteredList.length,
        },
      }
    },
  },
    // 添加/更新用户
  {
    url: '/acl/user/save',
    method: 'post',
    response: ({ body }) => {
      const newUser = {
        ...body,
        id: userList.length + 1,
        createTime: new Date().toISOString().split('T')[0],
        updateTime: new Date().toISOString().split('T')[0],
        status: 1,
      }
      userList.push(newUser)
      return { code: 200, data: null, message: '添加成功' }
    },
  },
  //修改用户
    {
    url: '/acl/user/update',
    method: 'put',
    response: ({ body }) => {
      const index = userList.findIndex((item) => item.id === body.id)
      if (index !== -1) {
        userList[index] = {
          ...userList[index],
          ...body,
          updateTime: new Date().toISOString().split('T')[0],
        }
      }
      return { code: 200, data: null, message: '更新成功' }
    },
  },
    // 删除用户
  {
    url: '/acl/user/remove/:id',
    method: 'delete',
    response: (request) => {
      const id = request.query.id
      if (!id) {
        return { code: 201, data: null, message: '参数错误' }
      }
      const index = userList.findIndex((item) => item.id === Number(id))
      if (index !== -1) {
        userList.splice(index, 1)
        return { code: 200, data: null, message: '删除成功' }
      }
      return { code: 201, data: null, message: '用户不存在' }
    },
  },


]